package com.mida.app.report.service.impl;

import com.mida.app.report.service.DetailCodingSystemService;
import com.mida.app.report.service.DocSupportAreaService;
import com.mida.bms.report.api.DocSupportAreaApi;
import com.mida.bms.report.vo.DocStructRequestVo;
import com.mida.bms.report.vo.DocStructVo;
import com.mida.bms.report.vo.PatientInfoParameterVo;
import com.mida.dms.mdm.vo.PullDownMenuVO;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 佐证区
 */
@Service
@Slf4j
@Transactional
public class DocSupportAreaServiceImpl implements DocSupportAreaService {

    private static final String DHUCVDT_0021 = "DHUCVDT0021";

    @Autowired
    private DocSupportAreaApi docSupportAreaApi;

    @Autowired
    private DetailCodingSystemService detailCodingSystemService;

    /**
     * 根据人标识以及 检查类型查询相关检查详情列表
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @Override
    public ListResult<List<DocStructRequestVo>> getDocSupportContentList(
            PatientInfoParameterVo vo) throws Exception {

        // 根据人标识以及 检查类型查询相关检查详情列表
        ListResult<List<DocStructRequestVo>> result =
                docSupportAreaApi.getDocSupportContentList(vo);
        // 设置检查类型名称
        setExamTypeName(result);

        // 返回结果
        return result;
    }

    /**
     * 设置检查类型名称
     * @param result
     * @throws Exception
     */
    private void setExamTypeName(ListResult<List<DocStructRequestVo>> result) throws Exception {
        // 检查详情列表不为空的场合
        if (result != null && result.getData() != null && result.getData().size() > 0) {

            // 取的列表结果集
            List<DocStructRequestVo> list = result.getData();

            // 根据DHUCVDT0021标识，查询检查类型编码体系
            List<PullDownMenuVO> listExamServiceType
                    = detailCodingSystemService.drawer(
                            DHUCVDT_0021, null, false);

            // 设置检查类型map
            Map<Long, String> map = new HashMap<>();
            listExamServiceType.stream().forEach(pullDownMenuVO -> {
                if (map.get(Long.valueOf(pullDownMenuVO.getCode())) == null) {
                    map.put(Long.valueOf(pullDownMenuVO.getCode()), pullDownMenuVO.getValue());
                }
            });

            // 根据检查类型soid，取得对应展示内容
            for (DocStructRequestVo d : list) {
                DocStructVo dsv =  d.getDocStructVo();
                String examTypeName = map.get(dsv.getExamTypeSoid());
                dsv.setExamTypeName(examTypeName);
            }
        }
    }
}
